package com.xl.studentpass.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xl.studentpass.entity.CommentEntity;
import com.xl.studentpass.entity.vo.CommentVo;
import com.xl.studentpass.mapper.CommentMapper;
import com.xl.studentpass.service.CommentService;
import com.xl.studentpass.utils.LocalDateUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

/**
 * @author: xl
 * @createTime: 2023/6/8 14:29:53
 * @program: student-pass
 * @e-mail: 2199396150@qq.com
 * @description:
 */
@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, CommentEntity> implements CommentService {

    private final String NAME = this.getClass().getName();

    @Override
    public Integer createComment(CommentVo commentVo) {

        if (ObjectUtils.isEmpty(commentVo)) {
            log.error(NAME + "添加评论时传入评论内容为空");
            return -1;
        }

        CommentEntity comment = new CommentEntity();

        BeanUtil.copyProperties(commentVo, comment);
        String nowDate = LocalDateUtils.getLocalDateTimeStr();

        comment.setCreateTime(nowDate);
        comment.setOperaTime(nowDate);
        comment.setValid(0);

        boolean b = this.saveOrUpdate(comment);
        if (!b) {
            log.error(NAME + "添加评论时插入数据失败");
            return -1;
        }
        return comment.getId();
    }

    @Override
    public Integer deleteComment(Integer commentId) {
        if (ObjectUtils.isEmpty(commentId)) {
            log.error(NAME + "删除评论时传入数据为空");
            return -1;
        }
        boolean b = this.removeById(commentId);
        if (!b) {
            log.error(NAME + "删除评论时数据库写入失败");
        }
        return commentId;
    }
}
